import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:winkai/res/app_colors.dart';
import 'package:winkai/widget/custom_style_bg.dart';
import 'package:winkai/widget/rounded_diagonal_path_clipper.dart';

void showAlertDialog(
  BuildContext context, {
  String? title,
  String? message,
  String? cancelText,
  EdgeInsetsGeometry? padding,
  String? confirmText,
  bool hasCancel = true, //能否点击外部或者按返回按钮取消dialog
  bool hasShowCancel = true, //是否显示取消按钮
  bool confirmAutoDismiss = true, //点击确认按钮是否关闭dialog
  bool cancelAutoDismiss = true, //点击取消按钮是否关闭dialog
  VoidCallback? cancelCallback,
  VoidCallback? confirmCallback,
  Color? messageTextColor,
  TextAlign? messageTextAlign,
}) {
  showDialog(
      context: context,
      barrierDismissible: hasCancel,
      builder: (_) {
        return WillPopScope(
          onWillPop: () async {
            return Future.value(hasCancel);
          },
          child: AlertDialog(
            title: title,
            message: message,
            padding: padding,
            cancelText: cancelText,
            confirmText: confirmText,
            hasShowCancel: hasShowCancel,
            confirmAutoDismiss: confirmAutoDismiss,
            cancelAutoDismiss: cancelAutoDismiss,
            cancelCallback: cancelCallback,
            confirmCallback: confirmCallback,
            messageTextColor: messageTextColor,
            messageTextAlign: messageTextAlign,
          ),
        );
      });
}

class AlertDialog extends StatelessWidget {
  final String? title;
  final String? message;
  final String? cancelText;
  final String? confirmText;
  final EdgeInsetsGeometry? padding;
  final bool hasShowCancel; //是否显示取消按钮
  final bool confirmAutoDismiss; //确认按钮点击即消失
  final bool cancelAutoDismiss; //取消按钮点击即消失
  final VoidCallback? cancelCallback;
  final VoidCallback? confirmCallback;
  final Color? messageTextColor;
  final TextAlign? messageTextAlign;

  const AlertDialog({
    super.key,
    this.title,
    this.message,
    this.cancelText,
    this.confirmText,
    this.padding,
    this.hasShowCancel = true,
    this.confirmAutoDismiss = true,
    this.cancelAutoDismiss = true,
    this.cancelCallback,
    this.confirmCallback,
    this.messageTextColor,
    this.messageTextAlign,
  });

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          ClipPath(
            clipper: RoundedDiagonalPathClipper(),
            child: Container(
              width: 306.w,
              decoration: const BoxDecoration(color: Colors.white),
              padding: padding ??
                  EdgeInsets.symmetric(
                    horizontal: 24.w,
                    vertical: 29.w,
                  ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  if (title?.isNotEmpty == true)
                    Flexible(
                        child: Text(
                      title ?? '',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontSize: 18.sp,
                          fontWeight: FontWeight.w600,
                          color: Colors.black),
                    )),
                  if (message?.isNotEmpty == true && title?.isNotEmpty == true)
                    SizedBox(height: 5.w),
                  if (message?.isNotEmpty == true)
                    Flexible(
                      flex: 2,
                      child: Text(
                        message ?? '',
                        textAlign: messageTextAlign ?? TextAlign.center,
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: messageTextColor ?? AppColors.color_99,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
                  SizedBox(height: 22.w),
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      if (hasShowCancel)
                        Flexible(
                          child: _buildCancel(context),
                        ),
                      if (hasShowCancel) SizedBox(width: 20.w),
                      if (hasShowCancel)
                        Flexible(
                          child: _buildConfirm(context),
                        ),
                      if (!hasShowCancel) _buildConfirm(context)
                    ],
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildCancel(BuildContext context) {
    return CupertinoButton(
      borderRadius: BorderRadius.zero,
      padding: EdgeInsets.zero,
      minSize: null,
      child: CustomStyleBg(
        height: 40.w,
        width: double.infinity,
        alignment: Alignment.center,
        padding: EdgeInsets.only(left: 8.w, right: 8.w),
        color: Colors.white,
        borderColor: AppColors.color_b3,
        child: Text(
          cancelText ?? 'Cancel',
          style: TextStyle(
            color: AppColors.color_4d,
            fontSize: 14.sp,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
      onPressed: () {
        if (cancelAutoDismiss) {
          Get.back();
        }
        cancelCallback?.call();
      },
    );
  }

  Widget _buildConfirm(BuildContext context) {
    return CupertinoButton(
      borderRadius: BorderRadius.zero,
      padding: EdgeInsets.zero,
      minSize: null,
      child: CustomStyleBg(
        height: 40.w,
        width: hasShowCancel ? double.infinity : null,
        alignment: Alignment.center,
        padding: EdgeInsets.only(
            left: hasShowCancel ? 8 : 80, right: hasShowCancel ? 8 : 80),
        color: const Color(0xffDFFF32),
        child: Text(
          confirmText ?? 'Confirm',
          style: TextStyle(
            color: Colors.black,
            fontSize: 14.sp,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
      onPressed: () {
        if (confirmAutoDismiss) {
          Get.back();
        }
        confirmCallback?.call();
      },
    );
  }
}
